package command;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import scp_client.ScpClient;


public class CommandProcessorImpl implements CommandProcessor {



	public String process(String str) {
		if (str.equals("")) {
			return "Unreconized please reinput";
		}
		String scpPattern = "scp\\s\\S+\\s(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d):/";
		Pattern pattern = Pattern.compile("("+scpPattern+")|(ls)|(rm\\s\\.+)");
		Matcher matcher = pattern.matcher(str);
		if (matcher.find()) {
			String[] ss = matcher.group().split("\\s+");
			if (ss[0].equals("scp")) {
				try {
					String ip = ss[2].subSequence(0, ss[2].length()-2).toString();
					System.out.println();
					return new ScpClient().upload(ss[1],ip );
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				}
			if (ss[0].equals("ls")) {
				return "null";
			}
			if (ss.equals("rm")) {
				return "null";
			}
		}
		return "Unreconized  command";
	}
	
	public static void main(String[] args) {
		new CommandProcessorImpl().process("scp D:\\quiz\\customersid.xml 127.0.0.1:/");
	}
	
}
